package com.asiainfo.demo.dao.proxy;

import com.asiainfo.demo.dao.dbcon.IYogaCon;
import com.asiainfo.demo.dao.idao.iMember;
import com.asiainfo.demo.dao.impl.MemberImpl;
import com.asiainfo.demo.dao.roles.Member;

import java.util.ArrayList;

public class MemberProxy implements iMember {
    private IYogaCon con = null;
    private iMember memberDao = null;

    public MemberProxy() throws Exception {
        con = new IYogaCon();
        memberDao = new MemberImpl(con.getConnection());
    }

    @Override
    public boolean insertMember(Member member) throws Exception {
        boolean flag = false;

        if (memberDao.findMemberByID(member.getMemID()) == null) {
            try {
                flag = memberDao.insertMember(member);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("Primary Key Exists. Insert Failed.");
        }

        con.close();
        return flag;
    }

    @Override
    public ArrayList<Member> findMemberByID(int ID) throws Exception {
        ArrayList<Member> members = memberDao.findMemberByID(ID);
        con.close();

        return members;
    }

    @Override
    public ArrayList<Member> findMemberByName(String name) throws Exception {
        ArrayList<Member> members = memberDao.findMemberByName(name);
        con.close();

        return members;
    }

    @Override
    public ArrayList<Member> findMemberByJob(String job) throws Exception {
        ArrayList<Member> members = memberDao.findMemberByJob(job);
        con.close();

        return members;
    }

    @Override
    public ArrayList<Member> findMemberByAddr(String addr) throws Exception {
        ArrayList<Member> members = memberDao.findMemberByAddr(addr);
        con.close();

        return members;
    }

    @Override
    public ArrayList<Member> findAllMember() throws Exception {
        ArrayList<Member> members = memberDao.findAllMember();
        con.close();

        return members;
    }

    @Override
    public boolean changeValid(int memID, byte newStatus) throws Exception {
        boolean flag = false;

        if (memberDao.findMemberByID(memID) != null) {
            try {
                flag = memberDao.changeValid(memID, newStatus);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        con.close();

        return flag;
    }
}
